package com.devops.admin.graphql;

import com.devops.admin.model.DOSApplyYaml;
import com.devops.admin.service.DOSApplyYamlService;
import com.devops.admin.util.Assert;
import com.devops.admin.vo.DOSApplyYamlVo;
import com.devops.common.annotation.DOSLog;
import com.devops.common.utils.Message;
import com.devops.constant.DevOpsConstant;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @ClassName DOSApplyYamlDataFetcher
 * @Description TODO
 * @Author heyabo
 * @Date 2022/8/3 15:39
 * @Version 1.0
 **/
@DgsComponent
public class DOSApplyYamlDataFetcher {

    @Autowired
    private DOSApplyYamlService dosApplyYamlService;

    /**
     * @Author heyabo
     * @Description 通过yaml文件部署项目接口
     * @Date 16:49 2022/7/14
     * @Param [applyYaml]
     * @return com.devops.admin.po.DOSHelmOperationPo
     **/
    @DgsMutation
    @DOSLog(description = "通过yaml文件部署项目接口")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public Message applyByYaml(@InputArgument DOSApplyYamlVo applyYaml){

        return dosApplyYamlService.applyByYaml(applyYaml);
    }

    /**
     * @Author heyabo
     * @Description 通过yaml文件升级release
     * @Date 14:30 2022/8/2
     * @Param [applyYaml]
     * @return com.devops.common.utils.Message
     **/
    @DgsMutation
    @DOSLog(description = "通过yaml文件升级release")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public Message upgradeByYaml(@InputArgument DOSApplyYamlVo applyYaml){

        Assert.isTrue(applyYaml.getReleaseId() != null && applyYaml.getReleaseId() > 0,"releaseId不能为空");

        return dosApplyYamlService.upgradeByYaml(applyYaml);
    }

    /**
     * @Author heyabo
     * @Description 回滚到指定版本
     * @Date 11:14 2022/7/19
     * @Param []
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "回滚到指定版本")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public Message rollBackToHistory(@InputArgument DOSApplyYamlVo applyYaml){

        return dosApplyYamlService.rollBackHistory(applyYaml);
    }

    /**
     * @Author heyabo
     * @Description 删除yaml部署的项目
     * @Date 17:40 2022/7/25
     * @Param [applyYaml]
     * @return com.devops.common.utils.Message
     **/
    @DgsMutation
    @DOSLog(description = "删除yaml部署的项目")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public Message deleteByYaml(@InputArgument DOSApplyYamlVo applyYaml){

        return dosApplyYamlService.deleteByYaml(applyYaml);
    }

    /**
     * @Author heyabo
     * @Description 根据releaseId查询yaml文件信息
     * @Date 14:15 2022/7/26
     * @Param [releaseId]
     * @return com.devops.admin.model.DOSApplyYaml
     **/
    @DgsQuery
    @DOSLog(description = "根据releaseId查询yaml文件信息")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public DOSApplyYaml getYaml(@InputArgument Integer releaseId){

        return dosApplyYamlService.getLabels(releaseId);
    }
}
